package com.twu.biblioteca;

import java.io.BufferedReader;
import java.util.Date;

public class BibliotecaApp {

    private IOHandler ioHandler;

    private Catalog inStock;
    private Catalog outStock;

    public BibliotecaApp()   //Constructor for production
    {
        ioHandler = new IOHandler();
        inStock = new Catalog();
        inStock.addBookToCatalog(new Book("Book 1", "Author 1", new Date()));
        inStock.addBookToCatalog(new Book("Book 2", "Author 2", new Date()));
        outStock = new Catalog();
        outStock.addBookToCatalog(new Book("Book 3", "Author 3", new Date()));

    }

    public BibliotecaApp(BufferedReader inStream) // Constructor for testing purpose only
    {
        this.ioHandler = new IOHandler(inStream);
        inStock = new Catalog();
        inStock.addBookToCatalog(new Book("Book 1", "Author 1", new Date()));
        inStock.addBookToCatalog(new Book("Book 2", "Author 2", new Date()));
        outStock = new Catalog();
        outStock.addBookToCatalog(new Book("Book 3", "Author 3", new Date()));

    }

    public void printWelcomeMessage()
    {
        ioHandler.printlnToConsole("Welcome to Biblioteca!!!");
    }

    public void checkoutBook() {
        ioHandler.printlnToConsole("Enter the name of the book : ");
        String name= ioHandler.readString();
        Book res = inStock.searchByName(name);
        if(res!=null) {
            inStock.deleteBookFromCatalog(res);
            outStock.addBookToCatalog(res);
            ioHandler.printlnToConsole("Thank you! Enjoy the book");
        }
        else {
            ioHandler.printlnToConsole("That book is not available.");
        }
    }

    public void returnBook() {
        ioHandler.printlnToConsole("Enter the name of the book : ");
        String name= ioHandler.readString();
        Book res = outStock.searchByName(name);
        if(res!=null) {
            outStock.deleteBookFromCatalog(res);
            inStock.addBookToCatalog(res);
            ioHandler.printlnToConsole("Thank you for returning the book.");
        }
        else {
            ioHandler.printlnToConsole("That is not a valid book to return.");
        }
    }

    public void performSelectedOption(int optionNumber)
    {
        switch(optionNumber) {
            case 1: inStock.printBookList();
                break;
            case 2: checkoutBook();
                break;
            case 3: returnBook();
                break;
            case 4: // System.exit(0); //Production code
                ioHandler.printlnToConsole("Quit"); //Test code
                break;
        }
    }

}
